package com.example.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


/**
 * @ClassName LoginController
 * @Description TODO
 * @Author: XMK
 * @Date: 2020/5/20 10:38
 * @Version: 1.0
 */
@Controller
public class LoginController {

    @GetMapping("/login")
    public String login() {

        return "login";
    }
    @GetMapping("/logout")
    public String logout() {

        return "logout";
    }

    @PostMapping("/logout")
    public String logtou() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return "index";
    }
    @PostMapping("/login")
    public String doloign(String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
            return "account-info";
        }
        catch (AuthenticationException e)
        {
            e.printStackTrace();
            String msg = "用户或密码错误";
        }
        return "login";
    }

}
